package org.jrmerz.esri.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.ucdavis.gwt.gis.client.Debugger;
import edu.ucdavis.gwt.gis.client.GisClient;
import edu.ucdavis.gwt.gis.client.GisClient.GisClientLoadHandler;
import edu.ucdavis.gwt.gis.client.layers.DataLayer;
import edu.ucdavis.gwt.gis.client.layers.MapServerDataLayer;
import edu.ucdavis.gwt.gis.client.layout.LayerMenuCreateHandler;
import edu.ucdavis.gwt.gis.client.layout.LayerMenuItem;
import edu.ucdavis.gwt.gis.client.resources.GadgetResources;
import edu.ucdavis.gwt.gis.client.toolbar.button.AddFeaturesButton;
import edu.ucdavis.gwt.gis.client.toolbar.menu.AddLayerMenu;
import edu.ucdavis.gwt.gis.client.toolbar.menu.ExportMenu;
import edu.ucdavis.gwt.gis.client.toolbar.menu.HelpMenu;
import edu.ucdavis.gwt.gis.client.toolbar.menu.SaveLoadMenu;

public class GwtGisClientExample implements EntryPoint {
	
	GisClient mapClient = null;
	AddLayerMenu addLayer = null;
	
	public void onModuleLoad() {
		// send all exceptions to debugger
		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler(){
			@Override
			public void onUncaughtException(Throwable e) {
				Debugger.INSTANCE.catchException(e, "Search", "Uncaught exception in Example Project");
			}
		});
		
		// add a new menu item to each layer information popup panel
		GisClient.setLayerMenuCreateHandler(new LayerMenuCreateHandler(){
			@Override
			public LayerMenuItem onCreate(VerticalPanel menu, DataLayer dl) {
					BrowseMenuItem item = new BrowseMenuItem();
					item.init(dl);
					menu.add(item);
					return item;
			}
		});
		
		// create client
		mapClient = new GisClient();
		
		// set a print page
		mapClient.setPrintPage(new ExamplePrintPage());
		
		// load client
		mapClient.load(new GisClientLoadHandler(){
			@Override
			public void onLoad() {
				onClientReady();
			}
		});
	}
	
	public void onClientReady() {
		addLayer = new AddLayerMenu();
		addLayer.addItem(new AddFeaturesButton());
		mapClient.getToolbar().addToolbarMenu(addLayer);
		mapClient.getToolbar().addSpacer();
		
		mapClient.getToolbar().addToolbarMenu(new SaveLoadMenu());
		mapClient.getToolbar().addSpacer();
		
		// create the toolbar menu's to your liking
		mapClient.getToolbar().addToolbarMenu(new ExportMenu());
		mapClient.getToolbar().addSpacer();
		
		
		mapClient.getToolbar().addToolbarMenu(new HelpMenu());
		mapClient.getToolbar().addSpacer();
	}
	
	// custom layer menu item
	private class BrowseMenuItem extends LayerMenuItem {
		
		@Override
		public Image getIcon() {
			return new Image(GadgetResources.INSTANCE.server());
		}
		@Override
		public String getText() {
			return "Browse ArcGIS Server";
		}
		@Override
		public void onClick(DataLayer dataLayer) {
			addLayer.getServiceAdder().browseServer(((MapServerDataLayer) dataLayer).getMapLayer().getUrl(), 70, 50);
		}
	}
	
}
